using UnityEngine;

namespace XFFSM
{
    public class FSMTransitionFactory 
    {

        public static void CreateTransition(RuntimeFSMController controller,string fromStateName,string toStateName) {

            FSMStateNodeData toNode = controller.GetStateNodeData(toStateName);

            if (toNode.IsAnyState || toNode.IsEntryState || toNode.IsUpState )
            {
                string message = string.Format("状态:{0}不能添加过渡!", toNode.DisplayName);
                Debug.LogWarning(message);
                FSMEditorWindow.ShowNotification(message);
                return;
            }

            if (fromStateName.Equals(toStateName)) {
                return;
            }

            foreach (var item in controller.transitions)
            {
                if (item.fromStateName.Equals(fromStateName) && item.toStateName.Equals(toStateName) ) 
                {
                    string message = string.Format("过渡 {0} -> {1} 已经存在,请勿重复添加!", fromStateName, toStateName);

                    Debug.LogError(message);
                    FSMEditorWindow.ShowNotification(message);
                    return;
                }
            }

            FSMTransitionData transition = new FSMTransitionData();
            transition.fromStateName = fromStateName;
            transition.toStateName = toStateName;
            controller.AddTransition(transition);
            UnityEditor.EditorUtility.SetDirty(controller);
            UnityEditor.AssetDatabase.SaveAssets();

        }

        public static void DeleteTransition(RuntimeFSMController controller,FSMTransitionData transition) {
            controller.RemoveTransition(transition);
            UnityEditor.EditorUtility.SetDirty(controller);
            UnityEditor.AssetDatabase.SaveAssets();
        }

    }

}

